﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Utils;
using System.Diagnostics;

namespace Hydra.Shell.DelayObjects
{
    public delegate void DelayedObjectHistoryHandler(object sender, EventArgs<DelayedObjectHistoryItem> args);

    public class DelayedObjectTimeoutException : TimeoutException
    {
        public DelayedObjectHistoryItem HistoryItem { get; private set; }

        public DelayedObjectTimeoutException(DelayedObjectHistoryItem historyItem) : base("Delayed object timed out after " + ((TimeSpan) historyItem.TimeoutIfNoFollowupHistory).TotalMilliseconds.ToString() + " milliseconds.")
        {
            this.HistoryItem = historyItem;
        }
    }

    [DebuggerDisplay(" { HistoryInfo } ")]
    public class DelayedObjectHistoryItem
    {
        public IDelayedObject DelayedObject { get; set; }
        public string HistoryDescription { get; set; }
        public DateTime Timestamp { get; set; }
        public string StackTrace { get; set; }
        public TimeSpan? TimeoutIfNoFollowupHistory { get; set; }
        public DispatcherTimer Timer { get; set; }

        public string HistoryInfo
        {
            get
            {
                var info = string.Format("ObjectType:{0}, Description:\"{1}\", Timestamp:{2:HH:mm:ss.fffffffzz}, StackTrace:\r\n\r\n{3}", DelayedObject.GetType().Name, HistoryDescription, Timestamp, StackTrace);

                return info;
            }
        }

        public override string ToString()
        {
            return this.HistoryInfo;
        }
    }
}
